var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
require('express-async-errors');


var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
const apiRouter = require('./routes/api');
const { createSecretKey } = require('crypto');


var app = express();

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));



app.use('/users', usersRouter);
app.use('/', indexRouter);
app.use('/api', apiRouter);


app.use(function (err, req, res, next) {
    if (err.name === "UnauthorizedError") {
      if(/^\/api\/getUserInfo/.test(req.url)){
        res.json({
          flag:false,
          msg:'token失败'
        })
      }else{
        res.status(401).send("invalid token...");
      }
    } else {
      next(err);
    }
  });


module.exports = app;
